package cn.itsource.pethome.department.controller;

import cn.itsource.pethome.department.domain.ShopAuditLog;
import cn.itsource.pethome.department.domain.ShopVo;
import cn.itsource.pethome.department.service.IShopService;
import cn.itsource.pethome.department.domain.Shop;
import cn.itsource.pethome.department.query.ShopQuery;
import cn.itsource.pethome.util.AjaxResult;
import cn.itsource.pethome.util.BusinessException;
import cn.itsource.pethome.util.ExcelUtil;
import cn.itsource.pethome.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.insert(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.queryOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.pageQueryAll(query);
    }


    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.registerUser(shop);
            return AjaxResult.me();
        }catch (BusinessException e){
            return AjaxResult.error().setMsg(e.getLocalizedMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }

    }

    @PostMapping("/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.pass(shopAuditLog);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败！"+e.getMessage());
        }
    }
    @PostMapping("/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.reject(shopAuditLog);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败！"+e.getMessage());
        }
    }
    //导出Excel
    @GetMapping("/export")
    public void export( HttpServletResponse response){
        try {
            List<Shop> shops = shopService.queryAll();
            ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //导入Excel
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        List<Shop> list = ExcelUtil.importExcel(file,0,1,Shop.class);
        list.forEach(System.out::println);
    }

    /**
     * echarts柱形图
     * @return
     */
    @GetMapping("/echarts")
    public List<Integer> echarts(){
        List<ShopVo> echarts = shopService.echarts();
        List<Integer> counts = echarts.stream().map(ShopVo::getCountNum).collect(Collectors.toList());
        return counts;
    }
    /**
     * echarts条形对比图
     * @return
     */
    @GetMapping("/echarts1")
    public List<Integer> echarts1(){
        List<ShopVo> echarts = shopService.echarts1(new Date());
        List<Integer> counts = echarts.stream().map(ShopVo::getCountNum).collect(Collectors.toList());
        return counts;
    }
    @GetMapping("/typecunt")
    public List<Integer> typecunt(){

        return shopService.typecunt();
    }
}
